package com.video.videos.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.video.common.result.PageResult;
import com.video.videos.domain.dto.QueryPageDto;
import com.video.videos.domain.dto.QueryReplyDto;
import com.video.videos.domain.entity.Comment;
import com.video.videos.domain.entity.Reply;
import com.video.videos.mapper.ReplyMapper;
import com.video.videos.service.ICommentService;
import com.video.videos.service.IReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 回复表 服务实现类
 * </p>
 *
 * @author la
 * @since 2024-11-13
 */
@Service
@RequiredArgsConstructor
public class ReplyServiceImpl extends ServiceImpl<ReplyMapper, Reply> implements IReplyService {

    private final ICommentService commentService;


    /*
     * 根据id查询评论列表
     * */
    @Override
    public Object QueryReplyList(QueryReplyDto dto) {

        // 参数校验
        dto.validateParams();

        // 分页获取数据
        Page<Reply> page = lambdaQuery()
                .eq(Reply::getCommentId, dto.getCommentId())
                .page(new Page<>(dto.getPage(), dto.getPageSize()));

        return new PageResult(page.getTotal(), page.getRecords());
    }


    /*
     * 新增回复
     * */
    @Override
    public void addReply(Reply dto) {
        // 补充数据在回复表中
        dto.setCreateTime(System.currentTimeMillis());
        dto.setUpvote(0);

        save(dto);

        // 给评论表新增replySize
        UpdateWrapper<Comment> wrapper = new UpdateWrapper<Comment>()
                .eq("id", dto.getCommentId())
                .setSql("reply_size = reply_size + 1");
        commentService.update(wrapper);
    }
}
